package com.weixing.mall.provider.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.domain.AgentBonus;
import com.weixing.mall.provider.model.domain.AgentBonusIssue;
import com.weixing.mall.provider.model.dto.AgentBonusIssueDto;
import com.weixing.mall.provider.model.enums.AgentWithdrawAuditEnum;
import com.weixing.mall.provider.model.query.AgentBonusIssueQuery;
import com.weixing.mall.provider.model.query.AgentBonusQuery;
import com.weixing.mall.provider.model.vo.AgentBonusIssueVo;
import com.weixing.mall.provider.model.vo.AgentBonusVo;
import com.weixing.mall.provider.service.IAgentBonusIssueService;
import com.weixing.mall.provider.service.IAgentBonusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 代理商分红发放接口
 *
 * @author siashan
 * @since 1.0.1
 */
@RestController
@RequestMapping("agent/bonusissus")
public class AgentBonusIssueController extends BaseController {

    @Autowired
    private IAgentBonusIssueService agentBonusIssueService;

    @Autowired
    private IAgentBonusService agentBonusService;

    /**
     * 代理商分红发放情况列表
     *
     * @param query   查询条件
     * @return        分红发放列表
     */
    @GetMapping("")
    public Result< IPage<AgentBonusIssueVo>> List(AgentBonusIssueQuery query){
        SysAuth auth = AuthUtil.getSysExt();
        return ResultUtil.success(agentBonusIssueService.selectListOfPage(initMpPage(),query,auth));
    }

    /**
     * 可提现的分红列表,应用于新增分红提现上部分列表展示
     * @param agentId  代理id
     * @return         可提现的分红列表
     */
    @GetMapping("ablebounsList/{agentId}")
    public Result<IPage<AgentBonusVo>> ableBounsList(@PathVariable Long agentId){
        AgentBonusQuery agentBonusQuery =new AgentBonusQuery();
        agentBonusQuery.setAgentId(agentId);
        agentBonusQuery.setIsIssus(1);
        IPage<AgentBonusVo> ttList= agentBonusService.selectListOfPage(initMPage(),agentBonusQuery);
        /*List<BigDecimal> bougAmoutList=ttList.getRecords().stream().map(AgentBonus::getBonusAmount).collect(Collectors.toList());
        double sum = bougAmoutList.stream().mapToDouble(x -> x.doubleValue()).sum();*/
        return ResultUtil.success(ttList);
    }

    /**
     * 代理商分红提现保存
     *
     * @param dto    参数
     * @return       结果
     */
    @PostMapping("save")
    @NoRepeatSubmit()
    public Result save( AgentBonusIssueDto dto){
        SysAuth auth = AuthUtil.getSysExt();
        try{
            agentBonusIssueService.save(auth,dto);
        }catch (Exception e){
            return ResultUtil.error("提现金额不足，或出现未知异常请联系管理员");
        }

        return ResultUtil.success();
    }

    /**
     * 代理商分红提现审核
     *
     * @param id             id
     * @param auditResult    审核结果
     * @param auditRemark     审核备注
     * @return              结果
     */
    @PostMapping("audit/{id}")
    @NoRepeatSubmit()
    public Result audit(@SysInfo SysAuth auth, @PathVariable Long id, AgentWithdrawAuditEnum auditResult,String auditRemark){
        AgentBonusIssue issue = agentBonusIssueService.getById(id);
        if(issue.getAuditResult().intValue()==AgentWithdrawAuditEnum.ADOPT.getValue()){
            return ResultUtil.error("已经审核通过不允许重复");
        }
        agentBonusIssueService.audit(auth,auditResult,id,auditRemark);
        return ResultUtil.success();
    }

    /**
     * 分红提现详情--用于回显
     *
     * @param id    id
     * @return      提现详情
     */
    @PostMapping("bounsdetails/{id}")
    public Result bounsdetails(@PathVariable Long id){
        AgentBonusIssue  agentBonusIssue= agentBonusIssueService.getById(id);
        IPage<AgentBonus> ttList= agentBonusService.page(initMPage(),new QueryWrapper<AgentBonus>().eq("out_bill_id",id));
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("bounsList",ttList);
        result.put("bounsAmount",agentBonusIssue.getAmount());
        result.put("bonusIssueDetails",agentBonusIssue);
        return ResultUtil.success(result);
    }
}
